package com.example.keli.ui.search;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.keli.R;
import com.example.keli.api.RetrofitManager;
import com.example.keli.base.BaseActivity;
import com.example.keli.base.utils.DensityUtil;
import com.example.keli.ui.home.activity.ObjectDetailActivity;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;

public class SearchActivity extends BaseActivity {


    @BindView(R.id.iv_cancel)
    ImageView ivCancel;
    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.recycler_search)
    RecyclerView recyclerSearch;
    private List<SearchVO> mListData;
    private SearchAdapter mAdapter;

    @Override
    protected int layoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void bindData() {
//        setTitle("选择地区");
        abTitle.setText("搜索");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initRecycle();
    }

    private void initRecycle() {
        mListData = new ArrayList<>();
        mAdapter = new SearchAdapter(mListData, this);
        recyclerSearch.setLayoutManager(new LinearLayoutManager(this));
        recyclerSearch.setAdapter(mAdapter);
        recyclerSearch.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .color(ContextCompat.getColor(this, R.color.divider_line))
                .size(DensityUtil.dip2px(this, 1))
                .build());
    }

    @Override
    protected void bindListener() {
        super.bindListener();
        searchEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mListData.clear();
                mAdapter.notifyDataSetChanged();
                if (!TextUtils.isEmpty(s.toString())){
                    mAdapter.setKeyValue(s.toString());
                    loadDatas(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SearchVO searchVO = (SearchVO) adapter.getItem(position);
                if (searchVO != null){
                    ObjectDetailActivity.start(SearchActivity.this, String.valueOf(searchVO.getUid()), String.valueOf(searchVO.getAaid()));
                }
            }
        });
    }

    private void loadDatas(String key) {
        Map<String, String> map = new HashMap<>();
        map.put("searchInfo", key);
        RetrofitManager.ApiFunc(RetrofitManager.mApiService.searchInfo(map), new SubscriberListener<BaseResponse<List<SearchVO>>>() {
            @Override
            public void onSuccess(BaseResponse<List<SearchVO>> listBaseResponse) {
                mListData.addAll(listBaseResponse.data);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailer(String msg) {
                toast(msg);
            }

            @Override
            public void onTokenError() {

            }
        });
    }

    @OnClick(R.id.iv_cancel)
    public void onViewClicked() {
        finish();
    }
}
